import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { relative } from 'path'
 
// 引入
import Pages from  'vite-plugin-pages'
import Layouts from 'vite-plugin-vue-layouts'
import Components from 'unplugin-vue-components/vite'
import AutoImport from 'unplugin-auto-import/vite'
import { VantResolver } from '@vant/auto-import-resolver';
 
// https://vitejs.dev/config/
export default defineConfig({
  resolve: {
    alias: {
      '@': relative(__dirname, 'src'),
      "components": relative(__dirname, "src/components"),
      'vue': 'vue/dist/vue.esm-bundler.js'
    },
  },
  plugins: [
    vue(),
    Components({
      deep: true, //搜索子目录
      resolvers: [ 
        VantResolver() //vant按需加载
      ],
      extensions: ['vue'],
      dirs: ['src/components'], //按需加载的文件夹,
      dts: 'src/components.d.ts',
    }),
    Layouts({
      // 如果是默认 layouts文件夹，默认 default.vue文件，则不需要配置
      layoutsDirs: 'src/layouts', // 布局文件存放目录
      defaultLayout: 'index', //对应 src/layouts/index.vue
    }),
    Pages({
      dirs: "src/views", //需要生成路由的文件目录，默认文件夹为pages
      exclude: ["**/components/*.vue"], //排除components目录下的.vue文件
      // extendRoute(route, parent) {
      //   if (route.path === '/') return {
      //       ...route,
      //       redirect: 'login'
      //   } //给默认路由加一个redirect，默认为index.vue
      // },
      // importMode: "async"
    }),
    AutoImport({ // 使用
      imports: ['vue', 'vue-router','pinia'],
      resolvers: [VantResolver()],
      dts: 'src/auto-import.d.ts',
      // // 如有用到eslint记得加上写段，没有用到可以忽略
      // eslintrc: {
      //   enabled: true,
      // },
    }),
  ],
  
  server: {
    host: '0.0.0.0', //显示当前局域网
    // open: false, //是否自动启动浏览器
    proxy: {
      //设置代理请求 当代理商识别你的请求如果前缀是 /api的话 就会自动转移
      '/shop': {
        // target: 'http://www.fast.com/shop',
        target: 'http://fast.zx.sleep-888.com/shop',
        changeOrigin:true,  //跨域
        //替换掉api前缀 防止多个api地址
        rewrite: (path) => path.replace(/^\/shop/, '')
      },
      '/uploads': {
        // target: 'http://www.fast.com/uploads',
        target: 'http://fast.zx.sleep-888.com/uploads',
        changeOrigin:true,  //跨域
        //替换掉api前缀 防止多个api地址
        rewrite: (path) => path.replace(/^\/uploads/, '')
      },
      // hmr: {
      //   overlay: false,
      // },
    },
  },
})
